package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.page.PageBeen;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.service.TbSpecificationService;
import com.dongdongshop.vo.SpecVO;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("specification")
public class TbSpecificationController {
    @Reference
    private TbSpecificationService tbSpecificationService;

    @RequestMapping("jeep")
    public String jeep() {
        return "admin/specification";
    }

    @RequestMapping("getSpecification")
    @ResponseBody
    public Result getSpecification(String specName, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize) {
        try {
            PageBeen<TbSpecification> pageBeen = tbSpecificationService.getSpecification(specName, pageNum, pageSize);
            return Result.response(ResultEnum.SUCCESS).setData(pageBeen);
        } catch (Exception e) {
            e.printStackTrace();
            PageBeen<TbSpecification> pageBeen = tbSpecificationService.getSpecification(specName, pageNum, pageSize);
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @RequestMapping("addInfo")
    @ResponseBody
    public Result addInfo(TbSpecification tbSpecification, String options) {
        List<TbSpecificationOption> tbsp = JSONObject.parseArray(options, TbSpecificationOption.class);
        int i = tbSpecificationService.addInfo(tbSpecification, tbsp);
        return Result.response(ResultEnum.SUCCESS);
    }

    @RequestMapping("deleteInfo/{ids}")
    @ResponseBody
    public Result deleteInfo(@PathVariable("ids") Integer[] ids) {
        int i = tbSpecificationService.deleteInfo(ids);
        return Result.response(ResultEnum.SUCCESS);
    }

    @RequestMapping("toUpdate/{id}")
    @ResponseBody
    public Result toUpdate(@PathVariable("id") Long id) {
        SpecVO specVO = tbSpecificationService.toUpdate(id);
        return Result.response(ResultEnum.SUCCESS).setData(specVO);
    }

    @RequestMapping("updateInfo")
    @ResponseBody
    public Result updateInfo(TbSpecification tbSpecification, String options) {
        List<TbSpecificationOption> tbsp = JSONObject.parseArray(options, TbSpecificationOption.class);
        int i = tbSpecificationService.updateInfo(tbSpecification, tbsp);
        return Result.response(ResultEnum.SUCCESS);
    }
}
